import { defineStore } from 'pinia'
export const useStoreMenu = defineStore('menuList', {
  state: () => ({
    //存放菜单
    menuList: [],
    backup_layout: [],
    backup_portlet: [],
    x: null,
    y: null, // 控制当前显示的组件
  }),
  actions: {
    reGet(list) {
      this.menuList = list
    },
    setCurrentItem(x, y) {
      this.x = x
      this.y = y
    },
    backup() {
      this.backup_layout = this.menuList[this.x].menu[this.y].layout
      this.backup_portlet = this.menuList[this.x].menu[this.y].portlets
    },
    clearLayAndPor() {
      this.menuList[this.x].menu[this.y].layout = []
      this.menuList[this.x].menu[this.y].portlets = []
    },
    changePrams(n, m, key, val) {
      let x, y
      if (n !== null && m !== null) {
        x = n
        y = m
      } else {
        x = this.x
        y = this.y
      }
      this.menuList[x].menu[y].params[key] = val
    },
    addLayout(lyt) {
      lyt.params = {}
      lyt.params.parentId = this.menuList[this.x].menu[this.y].id
      this.menuList[this.x].menu[this.y].layout = [lyt]
    },
    addPort(pt) {
      this.menuList[this.x].menu[this.y].portlets.push(pt)
    },
    addPage(index, item) {
      this.menuList[index].menu.push(item)
    },
  },
  getters: {
    // 自动将返回类型推断为数字
    current_select: (state) => {
      let cur
      if (state.x !== null && state.y !== null) {
        cur = state.menuList[state.x].menu[state.y]
      } else {
        cur = null
      }
      return cur
    },
  },
})
